package com.dengyafei.service.impl;

import com.dengyafei.bean.EnterpriseEntity;
import com.dengyafei.bean.PositionEntity;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.domin.dto.*;
import com.dengyafei.domin.vo.*;
import com.dengyafei.helper.BeanHelper;
import com.dengyafei.mapper.EnterpriseEntityMapper;
import com.dengyafei.mapper.PositionEntityMapper;
import com.dengyafei.mapper.PreferEntityMapper;
import com.dengyafei.result.PageEntity;
import com.dengyafei.result.SearchPositionEntity;
import com.dengyafei.service.PositionService;
import com.dengyafei.utils.JwtUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.javassist.util.proxy.ProxyObjectInputStream;
import org.apache.tomcat.util.net.openssl.OpenSSLUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 邓亚非
 */
@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    PositionEntityMapper positionEntityMapper;
    @Autowired
    EnterpriseEntityMapper enterpriseEntityMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    PreferEntityMapper preferEntityMapper;

    /**
     * 查询该hr发布的职位
     *
     * @param pageVo
     * @return
     */
    @Override
    public PageEntity<PositionEntity> queryByHid(PageVo pageVo) {
//        查询hid
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
//        分页
        Page<Object> page = PageHelper.startPage(pageVo.getCurrent(), pageVo.getPageSize());
        List<PositionEntity> positionEntities = positionEntityMapper.queryByHid(enterpriseEntity.getHid());
        PageEntity<PositionEntity> pageEntity = new PageEntity<>();
        pageEntity.setCurrent(pageVo.getCurrent());
        pageEntity.setPageSize(pageVo.getPageSize());
        pageEntity.setPages(page.getPages());
        pageEntity.setTotalCounts(page.getTotal());
        pageEntity.setList(positionEntities);
        return pageEntity;
    }

    /**
     * 添加一个职位
     *
     * @param positionInsertVO
     * @return
     */
    @Override
    public Integer insertOnePosition(PositionInsertVO positionInsertVO) {
//        获取hid
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
        PositionEntity positionEntity = BeanHelper.copyProperties(positionInsertVO, PositionEntity.class);
        positionEntity.setHid(enterpriseEntity.getHid());
        Integer insert = positionEntityMapper.insert(positionEntity);
        if (!insert.equals(RecommendConstant.OPERATION_SUCCESS)) {
            return RecommendConstant.POSITION_ADD_FAILURE;
        }
        return insert;
    }

    /**
     * 根据名称和hid查询职位信息
     *
     * @param positionQueryByNameVO
     * @return
     */
    @Override
    public PageEntity<PositionEntity> queryByName(PositionQueryByNameVO positionQueryByNameVO) {
        PageEntity<PositionEntity> pageEntity = new PageEntity<>();
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
        PositionQueryByNameDTO positionQueryByNameDTO = BeanHelper.copyProperties(positionQueryByNameVO, PositionQueryByNameDTO.class);
        positionQueryByNameDTO.setHid(enterpriseEntity.getHid());
        Page<Object> page = PageHelper.startPage(positionQueryByNameVO.getCurrent(), positionQueryByNameVO.getPageSize());
        List<PositionEntity> positionEntities = positionEntityMapper.queryByNameAndHid(positionQueryByNameDTO);
        pageEntity.setCurrent(positionQueryByNameVO.getCurrent());
        pageEntity.setPageSize(positionQueryByNameVO.getPageSize());
        pageEntity.setPages(page.getPages());
        pageEntity.setTotalCounts(page.getTotal());
        pageEntity.setList(positionEntities);
        return pageEntity;
    }

    /**
     * 职位编辑
     *
     * @param
     * @return
     */
    @Override
    public Integer editPosition(PositionEditVO positionEditVO) {
        PositionEntity positionEntity = BeanHelper.copyProperties(positionEditVO, PositionEntity.class);
        Integer i = positionEntityMapper.updateByPrimaryKey(positionEntity);
        if (!i.equals(RecommendConstant.OPERATION_SUCCESS)) {
            return RecommendConstant.POSITION_EDIT_FAILURE;
        }
        return i;
    }

    /**
     * 删除职位
     *
     * @param positionDeleteVO
     * @return
     */
    @Override
    public Integer deletePosition(PositionDeleteVO positionDeleteVO) {
        return positionEntityMapper.deleteByPrimaryKey(positionDeleteVO.getPid());
    }


    /**
     * 根据职位名称查询职位
     *
     * @param positionQueryByNameVO
     * @return
     */
    @Override
    public PageEntity<PositionEntity> queryByCondition(PositionQueryByNameVO positionQueryByNameVO) {
        PageEntity<PositionEntity> pageEntity = new PageEntity<>();
        PositionSearchByNameDTO positionSearchByNameDTO = new PositionSearchByNameDTO();
        positionSearchByNameDTO.setSearchParams(positionQueryByNameVO.getSearchParams());
        Page<Object> page = PageHelper.startPage(positionQueryByNameVO.getCurrent(), positionQueryByNameVO.getPageSize());
        List<PositionEntity> positionEntities = positionEntityMapper.queryByName(positionSearchByNameDTO);
        pageEntity.setCurrent(positionQueryByNameVO.getCurrent());
        pageEntity.setPageSize(positionQueryByNameVO.getPageSize());
        pageEntity.setPages(page.getPages());
        pageEntity.setTotalCounts(page.getTotal());
        pageEntity.setList(positionEntities);
        return pageEntity;
    }

    /**
     * 加载岗位数据接口
     * @param positionLoadVO
     * @return
     */
    @Override
    public List<PositionEntity> queryByHid(PositionLoadVO positionLoadVO) {
        EnterpriseLoadDTO enterpriseLoadDTO = BeanHelper.copyProperties(positionLoadVO, EnterpriseLoadDTO.class);
        List<Integer> list = enterpriseEntityMapper.queryHidByCompany(enterpriseLoadDTO);
        return positionEntityMapper.queryByHidList(list);
    }

    /**
     * 用户评分前五的职位
     * @return
     */
    @Override
    public List<PreferScorePositionDTO> queryScorePosition() {
        List<Integer> integers = preferEntityMapper.queryScorePid();
        List<String> list = positionEntityMapper.queryScorePosition(integers);
        List<PreferScoreDTO> preferScoreDTOS = preferEntityMapper.queryPreferScore();
        List<PreferScorePositionDTO> preferScorePositionDTOS =new ArrayList<>();
        for (String s : list) {
            PreferScorePositionDTO preferScorePositionDTO = new PreferScorePositionDTO();
            preferScorePositionDTO.setName(s);
            preferScorePositionDTOS.add(preferScorePositionDTO);
        }
        for (int i = 0; i < preferScoreDTOS.size(); i++) {
            preferScorePositionDTOS.get(i).setScore(preferScoreDTOS.get(i).getScore());
        }
        return preferScorePositionDTOS;
    }

    /**
     * 平均薪资排名前五的城市
     * @return
     */
    @Override
    public List<PositionAverageSalaryCityDTO> queryPositionAverageSalaryCity() {
        return positionEntityMapper.queryPositionAverageSalaryCity();
    }

    /**
     * 薪资最高的前五个职位
     * @return
     */
    @Override
    public List<PositionMaxSalaryDTO> queryPositionMaxSalary() {
        return positionEntityMapper.queryPositionMaxSalary();
    }

    /**
     * 所有职位信息
     * @return
     */
    @Override
    public List<PositionEntity> queryPositions() {
        return positionEntityMapper.queryPositiones();
    }
}